// https://www.lintcode.com/problem/max-points-on-a-line/my-submissions

/**
 * Definition for a point.
 * struct Point {
 *     int x;
 *     int y;
 *     Point() : x(0), y(0) {}
 *     Point(int a, int b) : x(a), y(b) {}
 * };
 */

class Solution {
public:
    /**
     * @param points: an array of point
     * @return: An integer
     */
    int maxPoints(vector<Point> &points) {
        map<float, int> recordMap;
        int maxNum = 0;
        for (int i = 0; i < points.size(); ++i)
        {
            int duplicate = 1;  //注意初始化为1
            recordMap.clear(); //注意清空     
            recordMap[INT_MAX] = 0;
            //Input
            // [[0,0]]
            // Output
            // 0
            // Expected
            // 1            
            for (int j = 0; j < points.size(); ++j)
            {
                if (i == j) continue;
                if (points[i].x == points[j].x && points[i].y == points[j].y)
                {
                    duplicate++;
                    continue; //注意
                }
                float k = (points[i].x == points[j].x)? INT_MAX: 
                    (float)(points[j].y - points[i].y) / 
                    (points[j].x - points[i].x);
                recordMap[k]++;
            }
            map<float, int>::iterator iter = recordMap.begin();
            for (; iter != recordMap.end(); ++iter)
            {
                if (iter->second + duplicate > maxNum) 
                    maxNum = iter->second + duplicate;
            }
        }
        return maxNum;
    }
};